package zdh.avatar.base;

import java.io.File;

/**
 * 
 * @author Gem Seeker
 */
public class Util {
    
    public static final String au = "au";
    public static final String wav = "wav";
    public static final String aiff = "aiff";
    
    /**
     * Retrieves the extension name of a file.
     * 
     * @param file
     * @return file extension
     */
    public static String getExtension(File file){
        String extension = null;
        String s = file.getName();
        int i = s.lastIndexOf('.');
        
        if(i>0 && i<s.length()-1){
            extension = s.substring(i+1).toLowerCase();
        }
        
        return extension;
    }
    
    /**
     * Generates a proper file name.
     * 
     * @param str
     * @return proper file name
     */
    public static String getProperFilename(String str){
        StringBuilder newStr = new StringBuilder();
        char[] chrs = str.toCharArray();
        for(int i=0; i<chrs.length; i++){
            // allow only letters, space character and digits...
            if(Character.isLetter(chrs[i]) || 
                    Character.isSpaceChar(chrs[i]) || 
                    Character.isDigit(chrs[i])){
                newStr.append(chrs[i]);
            }
        }
        
        return newStr.toString();
    }
    
    public static String generateProperTabName(String str){
        // max of 20 characters, min of 20 characters...
        if(str.length() > 20){
            return str.substring(0, 17).concat("...");
        }else if(str.length() < 20){
            int i = str.length();
            String s = str;
            for(int x=i-1; x<=20; x++){
                s += " ";
            }
            return s;
        }else{
            return str;
        }
    }
}
